home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap12 / LivePalette / LivePalette.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  4.5 KB  |  188 lines

  1. //***********************************************************************
  2. //
  3. //  LivePalette.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include "LivePalette.h"
  9.  
  10. #define FONTHEIGHT 72
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_CREATE ()
  30.     ON_WM_ERASEBKGND ()
  31.     ON_WM_PAINT ()
  32.     ON_WM_TIMER ()
  33.     ON_WM_QUERYNEWPALETTE ()
  34.     ON_WM_PALETTECHANGED ()
  35.     ON_WM_DESTROY ()
  36. END_MESSAGE_MAP ()
  37.  
  38. CMainWindow::CMainWindow ()
  39. {
  40.     Create (NULL, "Palette Animation Demo");
  41. }
  42.  
  43. int CMainWindow::OnCreate (LPCREATESTRUCT lpcs)
  44. {
  45.     static BYTE bColorVals[8][3] = {
  46.         128, 128, 128,  // Dark Gray
  47.         0,     0, 255,  // Blue
  48.         0,   255,   0,  // Green
  49.         0,   255, 255,  // Cyan
  50.         255,   0,   0,  // Red
  51.         255,   0, 255,  // Magenta
  52.         255, 255,   0,  // Yellow
  53.         192, 192, 192   // Light gray
  54.     };
  55.  
  56.     if (CFrameWnd::OnCreate (lpcs) == -1)
  57.         return -1;
  58.  
  59.     CClientDC dc (this);
  60.     if ((dc.GetDeviceCaps (RASTERCAPS) & RC_PALETTE) == 0) {
  61.         MessageBox ("Palette animation is not supported on this device",
  62.             "Sorry!", MB_ICONINFORMATION | MB_OK);
  63.         return -1;
  64.     }
  65.  
  66.     struct {
  67.         LOGPALETTE lp;
  68.         PALETTEENTRY ape[7];
  69.     } pal;
  70.  
  71.     LOGPALETTE* pLP = (LOGPALETTE*) &pal;
  72.     pLP->palVersion = 0x300;
  73.     pLP->palNumEntries = 8;
  74.  
  75.     for (int i=0; i<8; i++) {
  76.         pLP->palPalEntry[i].peRed = bColorVals[i][0];
  77.         pLP->palPalEntry[i].peGreen = bColorVals[i][1];
  78.         pLP->palPalEntry[i].peBlue = bColorVals[i][2];
  79.         pLP->palPalEntry[i].peFlags = PC_RESERVED;
  80.     }
  81.  
  82.     m_palette.CreatePalette (pLP);
  83.  
  84.     SetTimer (1, 500, NULL);
  85.     return 0;
  86. }
  87.  
  88. void CMainWindow::OnTimer (UINT nTimerID)
  89. {
  90.     PALETTEENTRY pe[8];
  91.     m_palette.GetPaletteEntries (7, 1, pe);
  92.     m_palette.GetPaletteEntries (0, 7, &pe[1]);
  93.     m_palette.AnimatePalette (0, 8, pe);
  94. }
  95.  
  96. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  97. {
  98.     CRect rect;
  99.     GetClientRect (&rect);
  100.  
  101.     CPalette* pOldPalette;
  102.     pOldPalette = pDC->SelectPalette (&m_palette, FALSE);
  103.     pDC->RealizePalette ();
  104.  
  105.     DoBkgndFill (pDC, &rect);
  106.  
  107.     pDC->SelectPalette (pOldPalette, FALSE);
  108.     return TRUE;
  109. }
  110.  
  111. void CMainWindow::OnPaint ()
  112. {
  113.     CRect rect;
  114.     GetClientRect (&rect);
  115.  
  116.     CPaintDC dc (this);
  117.     DoDrawText (&dc, &rect);
  118. }
  119.  
  120. BOOL CMainWindow::OnQueryNewPalette ()
  121. {
  122.     CClientDC dc (this);
  123.     dc.SelectPalette (&m_palette, FALSE);
  124.  
  125.     UINT nCount;
  126.     if (nCount = dc.RealizePalette ())
  127.         Invalidate ();
  128.  
  129.     return nCount;
  130. }
  131.  
  132. void CMainWindow::OnPaletteChanged (CWnd* pFocusWnd)
  133. {
  134.     if (pFocusWnd != this) {
  135.         CClientDC dc (this);
  136.         dc.SelectPalette (&m_palette, FALSE);
  137.         if (dc.RealizePalette ())
  138.             Invalidate ();
  139.     }
  140. }
  141.  
  142. void CMainWindow::OnDestroy ()
  143. {
  144.     KillTimer (1);
  145. }
  146.  
  147. void CMainWindow::DoBkgndFill (CDC* pDC, CRect* pRect)
  148. {
  149.     CBrush* pBrush[8];
  150.     for (int i=0; i<8; i++)
  151.         pBrush[i] = new CBrush (PALETTEINDEX (i));
  152.  
  153.     int nWidth = pRect->Width ();
  154.     int nHeight = pRect->Height () / 8;
  155.  
  156.     CRect rect;
  157.     int y1, y2;
  158.  
  159.     for (i=0; i<8; i++) {
  160.         y1 = i * nHeight;
  161.         y2 = (i == 7) ? pRect->Height () : y1 + nHeight;
  162.         rect.SetRect (0, y1, nWidth, y2);
  163.         pDC->FillRect (&rect, pBrush[i]);
  164.     }
  165.  
  166.     for (i=0; i<8; i++)
  167.         delete pBrush[i];
  168. }
  169.  
  170. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  171. {
  172.     CFont font;
  173.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * FONTHEIGHT) / 72);
  174.  
  175.     font.CreateFont (nHeight, 0, 0, 0, FW_BOLD, TRUE, 0, 0,
  176.         DEFAULT_CHARSET, OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS,
  177.         DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, "Times New Roman");
  178.  
  179.     pDC->SetBkMode (TRANSPARENT);
  180.     pDC->SetTextColor (RGB (255, 255, 255));
  181.  
  182.     CFont* pOldFont = pDC->SelectObject (&font);
  183.     pDC->DrawText ("Hello, MFC", -1, pRect, DT_SINGLELINE | DT_CENTER |
  184.         DT_VCENTER);
  185.  
  186.     pDC->SelectObject (pOldFont);
  187. }
  188.